﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Tasks.UI.Models.Account;
using Tasks.Business;
using Tasks.Common.Entitys;
using System.Collections;
using Tasks.Core;
using Tasks.Common;
using Tasks.UI.Models;
using Tasks.UI.Models.Hotel;
using Tasks.UI.Models.TourGuide;
using Tasks.UI.Models.Shop;
using Tasks.UI.Models.SceneryModels;
using Tasks.Common.Enum;
using MyLib.Util;
namespace Tasks.UI.Controllers
{
    public class AccountController : BaseController
    {

        AccountBLL accountBll = BusinessFactory.Account;


        public ActionResult Main()
        {
            return View();
        }

        /// 登录
        public ActionResult Login(string account, string password)
        {
            throw new NotImplementedException();
        }

        /// 退出登录
        /// 
        [RequireLogin]
        public ActionResult Logout()
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// 用户选择器
        /// </summary>
        /// <returns></returns>
        public ActionResult UserPicker(UserPickerModel model)
        {
            model.PageSize = model.PageSize == 0 ? 10 : model.PageSize;
            model.PageIndex = model.PageIndex == 0 ? 1 : model.PageIndex;
            model.act = string.IsNullOrEmpty(model.act) ? "personal" : model.act;
            int rowCount = 0;
            PowerRole powerRole = new PowerRole();
            if (model.act == "personal")
            {
                //提取员工 账号 
                Hashtable condition = new Hashtable();
                condition["Name"] = model.Name;
                condition["MobileNo"] = null;
                condition["ProvinceID"] = 0;
                condition["CityID"] = 0;
                condition["AreaID"] = 0;
                condition["CategoryID"] = 0;
                condition["SourceID"] = 0;

                model.PersonalMemberList = accountBll.SearchPersonalMember(condition, Config.CompanyID, model.PageSize, model.PageIndex, out rowCount);
                if (model.PersonalMemberList == null)
                    model.PersonalMemberList = new List<PersonalMember>();
            }
            else
            {
                //提取 企业会员账号 
                Hashtable condition = new Hashtable();
                condition["RoleID"] = 0;
                condition["Name"] = model.Name;
                condition["CategoryID"] = 0;
                condition["ProvinceID"] = 0;
                condition["CityID"] = 0;
                condition["AreaID"] = 0;
                condition["EnterpriseType"] = 0;
                condition["EnterpriseMemberState"] = (int)EnterpriseMemberState.有效;
                model.EnterpriseList = accountBll.SearchEnterpriseMember(condition, Config.CompanyID, model.PageSize, model.PageIndex, out rowCount);
                if (model.EnterpriseList == null) model.EnterpriseList = new List<EnterpriseMember>();

                if (Config.ParentCompanyID != 0 && Config.ParentCompanyID != Config.CompanyID)
                {
                    EnterpriseMember ent = accountBll.GetEnterpriseMember(Config.ParentCompanyID);
                    model.EnterpriseList.Add(ent);
                }
            }
            model.RowCount = rowCount;
            return View(model);
        }
        /// <summary>
        /// 用户选择器(企业员工和企业会员)
        /// </summary>
        /// <returns></returns>
        public ActionResult UserPickeremployee(UserPickerModel model)
        {
            model.PageSize = model.PageSize == 0 ? 10 : model.PageSize;
            model.PageIndex = model.PageIndex == 0 ? 1 : model.PageIndex;
            model.act = string.IsNullOrEmpty(model.act) ? "employee" : model.act;
            int rowCount = 0;
            PowerRole powerRole = new PowerRole();
            if (model.act == "employee")
            {
                //提取员工 账号 
                model.AccountList = accountBll.SearchEmployee(model.Name, Config.CompanyID, model.PageSize, model.PageIndex, out rowCount);
                if (model.AccountList == null) model.AccountList = new List<Employee>();
            }
            else
            {
                //提取 企业会员账号 
                Hashtable condition = new Hashtable();
                condition["RoleID"] = 0;
                condition["Name"] = model.Name;
                condition["CategoryID"] = 0;
                condition["ProvinceID"] = 0;
                condition["CityID"] = 0;
                condition["AreaID"] = 0;
                condition["EnterpriseType"] = 0;
                condition["EnterpriseMemberState"] = (int)EnterpriseMemberState.有效;
                model.EnterpriseList = accountBll.SearchEnterpriseMember(condition, Config.CompanyID, model.PageSize, model.PageIndex, out rowCount);
                if (model.EnterpriseList == null) model.EnterpriseList = new List<EnterpriseMember>();

                if (Config.ParentCompanyID != 0 && Config.ParentCompanyID != Config.CompanyID)
                {
                    EnterpriseMember ent = accountBll.GetEnterpriseMember(Config.ParentCompanyID);
                    model.EnterpriseList.Add(ent);
                }
            }
            model.RowCount = rowCount;
            return View(model);
        }
        /// 修改密码
        [HttpGet, RequireLogin]
        public ActionResult ModifyPassword(ModifyPasswordModel model)
        {
            if (!string.IsNullOrWhiteSpace(model.Theme))
            {
                return View("~/Views/themes/" + model.Theme + "/Account/ModifyPassword.cshtml", model);
            }

            return View(model);
        }

        /// 修改密码
        [ActionName("ModifyPassword"), HttpPost, RequireLogin]
        public ActionResult DoModifyPassword(ModifyPasswordModel model)
        {
            Account account = accountBll.GetAccount(Config.CurrentOperator.UserID);
            ExecuteResult result;

            if (
                model.NewPassword != model.ConfirmPassword
                || account == null
                || SecurityUtil.EncryptToMD5(model.NewPassword) != account.Password)
            {
                model.Message = "旧密码不正确";
                model.MessageType = MessageType.error;
                return View(model);
            }

            result = accountBll.ModifyPassword(model.NewPassword, model.UserID);
            if (result.ResultNo == 1)
            {
                model.Message = "修改成功";
                model.MessageType = MessageType.correct;
            }
            else
            {
                model.Message = result.Message;
                model.MessageType = MessageType.error;
            }

            return View(model);
        }

        #region "员工相关"

        /// <summary>
        /// 显示员工账号列表管理界面
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [RequireLogin("employee", "查看员工")]
        public ActionResult EmployeeManage(EmployeeManageModel model)
        {
            #region "权限验证"

            Power power = new Power();
            if ((model.act == "delete" || model.act == "disable" || model.act == "enable")
                && !power.ValidatePower("employee", "员工操作"))
            {
                return Content("没有操作权限");
            }

            #endregion

            ExecuteResult result;
            switch (model.act)
            {
                case "delete"://删除
                    result = accountBll.SetEmployeeState(EmployeeState.无效, model.UserID);
                    break;
                case "disable"://禁用
                    result = accountBll.SetEmployeeState(EmployeeState.禁用, model.UserID);
                    break;
                case "enable": //启用
                    result = accountBll.SetEmployeeState(EmployeeState.有效, model.UserID);
                    break;
            }

            model.PageIndex = model.PageIndex == 0 ? 1 : model.PageIndex;
            int rowCount = 0;
            model.PageSize = model.PageSize == 0 ? 15 : model.PageSize;

            Hashtable condition = new Hashtable();
            condition["RoleID"] = 0;
            condition["Name"] = model.Name;
            condition["MobileNo"] = model.MobileNo;
            condition["DeptID"] = model.DeptID;
            model.AccountList = accountBll.SearchEmployee(condition, Config.CompanyID, model.PageSize, model.PageIndex, out rowCount);
            model.RowCount = rowCount;
            if (model.AccountList == null) model.AccountList = new List<Employee>();

            if (!string.IsNullOrWhiteSpace(model.Theme))
            {
                return View("~/Views/themes/" + model.Theme + "/Account/EmployeeManage.cshtml", model);
            }

            return View(model);
        }

        /// <summary>
        /// 显示员工账号编辑界面，用于添加或修改
        /// </summary>
        /// <returns></returns>
        [HttpGet, RequireLogin("employee", "员工操作")]
        public ActionResult EditEmployee(EmployeeEditModel model)
        {
            if (model.UserID != 0)
            {
                Employee employee = accountBll.GetEmployee(model.UserID);

                model.UserID = model.UserID;
                model.AccountNo = employee.AccountNo;

                model.Remark = employee.Remark;
                model.Birthday = employee.Birthday.ToShortDateString();
                model.Sex = employee.Sex;
                model.Name = employee.Name;
                model.AccountNo = employee.AccountNo;
                model.DeptID = employee.DeptID;
                model.DeptName = employee.DeptName;

                model.EMail = employee.EMail;
                model.MobileNo = employee.MobileNo;
                model.Address = employee.Contact.Address;
                model.PostalCode = employee.Contact.PostalCode;
                model.LinkmanFax = employee.Contact.Fax;
                model.LinkmanTel = employee.Contact.Tel;
                model.OfficeID = employee.OfficeID;

            }
            else
            {
                model.Birthday = DateTime.Now.ToShortDateString();
            }

            if (!string.IsNullOrWhiteSpace(model.Theme))
            {
                return View("~/Views/themes/" + model.Theme + "/Account/EditEmployee.cshtml", model);
            }
            return View(model);
        }

        /// <summary>
        /// 保存员工信息，操作包含添加或修改
        /// </summary>
        /// <returns></returns>
        [ActionName("EditEmployee"), HttpPost, RequireLogin("employee", "员工操作")]
        public ActionResult DoSaveEmployeeInfo(EmployeeEditModel model)
        {
            AccountBLL accountBLL = BusinessFactory.Account;
            ExecuteResult result = new ExecuteResult();

            #region "码修改"

            if (model.act == "changepassword")
            {
                if (!string.IsNullOrEmpty(model.Password))
                {
                    result = accountBll.ModifyPassword(model.Password, model.UserID);
                    model.Message = result.ResultNo == 1 ? "修改成功" : "修改失败";
                    model.MessageType = MessageType.correct;
                    return RedirectToAction("EditEmployee", model);
                }
                else
                {
                    model.Message = "修改失败,请输入新密码";
                    model.MessageType = MessageType.error;
                    return RedirectToAction("EditEmployee", model);
                }
            }

            #endregion

            Employee employee = new Employee();
            employee.Contact = new ContactInfo();

            if (model.UserID != 0)
                employee = accountBll.GetEmployee(model.UserID);
            else
            {
                //添加员工信息
                employee.CreateTime = DateTime.Now;
                employee.CreateUserID = Config.CurrentOperator.UserID;
                employee.UserType = (int)UserType.Employee;
                employee.LastLoginTime = DateTime.Now;
                employee.State = (int)EmployeeState.有效;
                employee.EmployeeState = (int)EmployeeState.有效;
                employee.Password = model.Password;
                employee.AccountNo = model.AccountNo;
            }


            employee.Birthday = Convert.ToDateTime(model.Birthday);
            employee.DeptID = model.DeptID;
            employee.Sex = model.Sex;
            employee.Remark = model.Remark;
            employee.OfficeID = model.OfficeID;

            employee.Name = employee.Contact.UserName = employee.Contact.Linkman = model.Name;
            employee.Contact.EMail = employee.Contact.LinkmanEmail = employee.EMail = model.EMail;
            employee.Contact.MobileNo = employee.Contact.LinkmanMobileNo = employee.MobileNo = model.MobileNo;
            employee.Contact.Address = employee.Contact.LinkmanAddress = model.Address;
            employee.Contact.Fax = employee.Contact.LinkmanFax = model.LinkmanFax;
            employee.Contact.Tel = employee.Contact.LinkmanTel = model.LinkmanTel;
            employee.Contact.PostalCode = model.PostalCode;
            employee.Contact.ProvinceID = model.ProvinceID;
            employee.Contact.CityID = model.CityID;
            employee.Contact.AreaID = model.AreaID;
            employee.OwnerID = Config.CompanyID;

            if (model.UserID == 0)//判断操作是添加还是修改，UserID为0添加，否则为修改
            {
                #region "验证数据"

                if (string.IsNullOrEmpty(model.Password))
                {
                    model.Message = "请输入密码!";
                    model.MessageType = MessageType.error;
                    return RedirectToAction("EditEmployee", model);
                }

                #endregion


                employee.AccountNo = model.AccountNo;
                result = accountBll.CreateEmployee(employee);
            }
            else
            {
                //修改员工信息
                result = accountBll.ModifyEmployee(employee);
            }

            if (result.ResultNo == 1)
            {
                if (!string.IsNullOrWhiteSpace(model.ReturnUrl))
                {
                    return Redirect(model.ReturnUrl);
                }
                else
                {
                    model.Message = "操作成功";
                    model.MessageType = MessageType.close;
                    return new CloseResult();
                    //return RedirectToAction("EditEmployee", model);
                }
            }
            else
            {
                model.Message = result.Message;
                model.MessageType = MessageType.error;
                return RedirectToAction("EditEmployee", model);
            }

        }

        [HttpGet]
        public ActionResult UserInfo(UserInfoModel model)
        {
            if (model.UserID != 0)
            {
                Employee employee = accountBll.GetEmployee(model.UserID);
                model.DeptName = employee.DeptName;
                model.AccountNo = employee.AccountNo;
                model.MobileNo = employee.MobileNo;
                model.LinkmanFax = employee.Contact.Fax;
                model.LinkmanTel = employee.Contact.Tel;
                model.QQnumber = employee.QQ;
                model.JobCode = employee.JobCode;
            }
            return View(model);
        }


        /// <summary>
        /// 个人资料查看编辑
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [ActionName("UserInfo"), HttpPost]
        public ActionResult doUserInfo(UserInfoModel model)
        {
            ExecuteResult result = new ExecuteResult();
            if (model.UserID != 0)
            {
                Employee employee = accountBll.GetEmployee(model.UserID);
                employee.MobileNo = model.MobileNo;
                employee.Contact.Fax = model.LinkmanFax;
                employee.Contact.Tel = model.LinkmanTel;
                employee.QQ = model.QQnumber;
                employee.JobCode = model.JobCode;
                result = accountBll.ModifyEmployee(employee);
            }
            if (result.ResultNo == 1)
            {
                if (!string.IsNullOrWhiteSpace(model.ReturnUrl))
                {
                    return Redirect(model.ReturnUrl);
                }
                else
                {
                    model.Message = "操作成功";
                    model.MessageType = MessageType.close;
                    return new CloseResult();
                }
            }
            else
            {
                model.Message = result.Message;
                model.MessageType = MessageType.error;
                return RedirectToAction("UserInfo", model);
            }
        }

        #endregion

        #region "个人会员"

        /// <summary>
        /// 个人会员管理
        /// 
        /// GET|POST   /Account/PersonalMemberManage
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [RequireLogin("personal", "查看会员")]
        public ActionResult PersonalMemberManage(UI.Models.Account.PersonalManageModel model)
        {
            ExecuteResult result;
            switch (model.act)
            {
                case "delete"://删除
                    result = accountBll.SetPersonalMemberState(Convert.ToInt32(PersonalMemberState.无效), model.UserID);
                    break;
                case "disable"://禁用
                    result = accountBll.SetPersonalMemberState(Convert.ToInt32(PersonalMemberState.禁用), model.UserID);
                    break;
                case "enable": //启用
                    result = accountBll.SetPersonalMemberState(Convert.ToInt32(PersonalMemberState.有效), model.UserID);
                    break;
            }
            AccountBLL account = BusinessFactory.Account;
            model.PageSize = model.PageSize == 0 ? 15 : model.PageSize;
            int rowCount = 0;
            if (model.PageIndex == 0)
            {
                model.PageIndex = 1;
            }
            Hashtable hta = new Hashtable();
            hta.Add("Name", model.Name);
            hta.Add("MobileNo", model.MobileNo);
            hta.Add("ProvinceID", model.ProvinceID);
            hta.Add("CityID", model.CityID);
            hta.Add("AreaID", model.AreaID);
            hta.Add("CategoryID", model.CategoryID);
            hta.Add("SourceID", model.SourceID);

            model.accountlist = account.SearchPersonalMember(hta, Config.CompanyID, model.PageSize, model.PageIndex, out rowCount);
            model.RowCount = rowCount;

            if (model.accountlist == null) model.accountlist = new List<PersonalMember>();

            return View(model);
        }

        /// <summary>
        /// 打开编辑个人会员信息窗口   /添加/编辑
        /// GET  /Account/EditPersonalMember?UserID=xxxx
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpGet, RequireLogin("personal", "会员操作")]
        public ActionResult EditPersonalMember(PersonalMemberEditModel model)
        {
            AccountBLL account = BusinessFactory.Account;
            PersonalMember member = new PersonalMember();
            if (model.UserID != 0)
            {

                member = account.GetPersonalMember(model.UserID);
                DictValue Category = BusinessFactory.BasedataBLL.GetDict(member.CategoryID);
                DictValue Source = BusinessFactory.BasedataBLL.GetDict(member.SourceID);
                model.CategoryID = member.CategoryID;
                model.Name = member.Name;
                model.AccountNo = member.AccountNo;
                model.Sex = member.Sex;
                string date = member.Birthday.ToShortDateString();
                model.Birthday = date;
                model.SourceName = Source != null ? Source.Name : "";
                model.SourceID = member.SourceID;
                model.MobileNo = member.MobileNo;
                model.IDCard = member.IDCard;
                model.EMail = member.EMail;
                model.ProvinceID = Convert.ToInt32(member.Contact.ProvinceID);
                model.CityID = Convert.ToInt32(member.Contact.CityID);
                model.AreaID = Convert.ToInt32(member.Contact.AreaID);
                model.Address = member.Contact.Address;
                model.PostalCode = member.Contact.PostalCode;
                model.Remarks = member.Remarks;
                model.CustomerServiceID = member.CustomerServiceID;
                if (model.CustomerServiceID > 0)
                {
                    model.CustomerServiceIDName = account.GetAccount(model.CustomerServiceID).Name;
                }

                model.SalesmanID = member.SalesmanID;
                if (model.SalesmanID > 0)
                {
                    model.SalesmanIDName = account.GetAccount(model.SalesmanID).Name;
                }
            }
            return View(model);
        }

        /// <summary>
        /// //保存个人会员信息窗口   /添加/编辑
        ///POST  /Account/EditPersonalMember?UserID=xxxx
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [ActionName("EditPersonalMember"), HttpPost, RequireLogin("personal", "会员操作"), ValidateInput(false)]
        public ActionResult DoSavePersonalMemberInfo(PersonalMemberEditModel model)
        {
            AccountBLL account = BusinessFactory.Account;
            PersonalMember member = new PersonalMember();
            if (model.UserID != 0)
                member = account.GetPersonalMember(model.UserID);
            else
            {
                member.CreateUserID = Config.CurrentOperator.UserID;
                member.State = (int)PersonalMemberState.有效;
                member.Password = model.Password;
                member.CreateTime = DateTime.Now;
                member.UserType = (int)UserType.Personal;
                member.UserNo = "1";
                member.LastLoginTime = DateTime.Now;
                member.PersonalMemberState = (int)PersonalMemberState.有效;
            }
            member.CustomerServiceID = model.CustomerServiceID;
            member.SalesmanID = model.SalesmanID;
            member.Name = model.Name;
            member.AccountNo = model.AccountNo;
            member.Sex = model.Sex;
            if (model.Birthday == null)
            {
                member.Birthday = DateTime.Now;
            }
            else
            {
                member.Birthday = Convert.ToDateTime(model.Birthday);
            }
            IList<DictValue> cats = BusinessFactory.BasedataBLL.Per_Member_Cat();//会员类别
            if (cats != null && cats.Count != 0)
            {
                member.CategoryID = cats.Where(t => t.Name == "个人会员").ElementAt(0).ID;
            }
            member.SourceID = model.SourceID;
            member.MobileNo = model.MobileNo;
            member.IDCard = model.IDCard;
            member.EMail = model.EMail;
            member.Contact.ProvinceID = model.ProvinceID;
            member.Contact.CityID = model.CityID;
            member.Contact.AreaID = model.AreaID;
            member.Contact.Address = model.Address;
            member.Contact.PostalCode = model.PostalCode;
            member.Remarks = model.Remarks;
            member.OwnerID = Config.CompanyID;

            ExecuteResult result;
            if (model.UserID == 0)
            {
                if (account.GetAccountByAccountNo(model.AccountNo) != null)
                {
                    model.Message = "已经存在此账号";
                    model.MessageType = MessageType.error;
                    return View(model);
                }
                else
                {
                    //if (account.GetAccountByMobileNo(model.MobileNo) != null)
                    //{
                    //    model.Message = "手机号码已经被用过";
                    //    model.MessageType = MessageType.error;
                    //    return View(model);
                    //}
                }
                if (!string.IsNullOrEmpty(model.Password))
                {
                    result = account.CreatePersonalMember(member);
                    if (result.ResultNo == 1)
                    {
                        model.Message = "添加成功";
                        model.MessageType = MessageType.correct;
                    }
                }
                else
                {
                    model.PasswordError = "密码不能为空";
                    return View(model);
                }
            }
            else
            {
                result = account.ModifyPersonalMember(member);
                if (result.ResultNo == 1)
                {
                    model.Message = "修改成功";
                    model.MessageType = MessageType.correct;
                }
            }

            return RedirectToAction("EditPersonalMember", new { UserID = member.UserID, Message = model.Message, MessageType = model.MessageType });
        }

        #endregion

        #region "企业会员"

        /// <summary>
        /// 企业会员管理
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        //[RequireLogin("enterprise", "查看企业")]
        public ActionResult EnterpriseMemberManage(EnterpriseManageModel model)
        {
            #region "事件处理"

            Power power = new Power();
            if ((model.act == "delete" || model.act == "disable" || model.act == "enable")
                && !power.ValidatePower("enterprise", "企业操作"))
            {
                return Content("你没有操作权限");
            }

            ExecuteResult result;
            switch (model.act)
            {
                case "delete"://删除
                    result = accountBll.SetEnterpriseMemberState(EnterpriseMemberState.无效, model.UserID);
                    break;
                case "disable"://禁用
                    result = accountBll.SetEnterpriseMemberState(EnterpriseMemberState.禁用, model.UserID);
                    break;
                case "enable": //启用
                    result = accountBll.SetEnterpriseMemberState(EnterpriseMemberState.有效, model.UserID);
                    break;
            }

            #endregion

            AccountBLL accountbll = BusinessFactory.Account;
            Hashtable condition = new Hashtable();

            model.PageIndex = model.PageIndex == 0 ? 1 : model.PageIndex;
            model.PageSize = model.PageSize == 0 ? 15 : model.PageSize;
            int rowCount = 0;
            condition.Add("RoleID", 0);
            condition.Add("Name", model.Name);
            condition.Add("CategoryID", model.CategoryID);
            condition.Add("ProvinceID", model.ProvinceID);
            condition.Add("CityID", model.CityID);
            condition.Add("AreaID", model.AreaID);
            condition.Add("EnterpriseMemberState", 0);
            condition.Add("EnterpriseType", model.EnterpriseType);

            model.EnterpriseMemberlist = accountbll.SearchEnterpriseMember(condition, Config.CompanyID, model.PageSize, model.PageIndex, out rowCount);
            model.RowCount = rowCount;
            if (model.EnterpriseMemberlist == null) model.EnterpriseMemberlist = new List<EnterpriseMember>();


            return View(model);
        }

        /// <summary>
        /// 编辑企业会员信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        //[HttpGet, RequireLogin("enterprise", "企业操作")]
        public ActionResult EditEnterpriseMember(EnterpriseEditModel model)
        {
            AccountBLL account = BusinessFactory.Account;
            EnterpriseMember ent = new EnterpriseMember();

            if (model.UserID != 0)
            {
                ent = account.GetEnterpriseMember(model.UserID);
                DictValue category = BusinessFactory.BasedataBLL.GetDict(ent.CategoryID);
                model.Name = ent.Name;

                model.AccountNo = ent.AccountNo;
                model.MobileNo = ent.MobileNo;
                model.EMail = ent.EMail;
                model.UserNo = ent.UserNo;
                model.ProvinceID = ent.Contact.ProvinceID;
                model.CityID = ent.Contact.CityID;
                model.AreaID = ent.Contact.AreaID;
                model.Address = ent.Contact.Address;
                model.PostalCode = ent.Contact.PostalCode;

                model.BusinessCertificate = ent.BusinessCertificate;
                model.CategoryID = ent.CategoryID;
                model.CategoryName = category != null ? category.Name : "";
                model.EntepriseMemberState = ent.EnterpriseMemberState;
                model.LinkmanFax = ent.Contact.LinkmanFax;
                model.IsDistribution = ent.IsDistribution == 1;
                model.Linkman = ent.Contact.Linkman;
                model.LinkmanMobileNo = ent.Contact.LinkmanMobileNo;
                model.LinkmanTel = ent.Contact.LinkmanTel;
                model.LinkmanEmail = ent.Contact.LinkmanEmail;
                model.Alias = ent.Alias;
                model.MaxCredit = ent.MaxCredit;
                model.MemberLevel = ent.MemberLevel;
                model.ParentID = ent.ParentID;
                model.PaymentMode = ent.PaymentMode;
                model.Remark = ent.Remark;
                model.WebSiteUrl = ent.WebSiteUrl;
                model.CustomerServiceID = ent.CustomerServiceID;
                model.PaymentMode = ent.PaymentMode;
                model.EnterpriseType = ent.EnterpriseType;

                if (model.CustomerServiceID > 0)
                {
                    model.CustomerServiceIDName = account.GetAccount(model.CustomerServiceID).Name;
                }
                model.SalesmanID = ent.SalesmanID;
                if (model.SalesmanID > 0)
                {
                    model.SalesmanIDName = account.GetAccount(model.SalesmanID).Name;
                }
            }

            return View(model);
        }

        /// <summary>
        /// 保存企业会员信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [ActionName("EditEnterpriseMember"), HttpPost, ValidateInput(false)]
        public ActionResult DoSaveEnterpriseMember(EnterpriseEditModel model)
        {
            AccountBLL account = BusinessFactory.Account;
            EnterpriseMember ent = new EnterpriseMember();
            ent.Contact = new ContactInfo();

            #region "修改密码"

            if (model.act == "ChagePassword")
            {
                if (model.Password != null && model.Password != "")
                {
                    account.ModifyPassword(model.Password, model.UserID);
                    model.ChagePassworderror = "修改成功";
                }
                else
                {
                    model.ChagePassworderror = "请输入密码";
                }

                return View(model);

            }

            #endregion

            if (model.UserID != 0)
                ent = account.GetEnterpriseMember(model.UserID);

            ent.Name = model.Name;
            ent.CategoryID = model.CategoryID;

            ent.EMail = model.EMail;
            ent.MobileNo = model.MobileNo;


            ent.WebSiteUrl = model.WebSiteUrl;
            ent.Alias = model.Alias;
            ent.BusinessCertificate = model.BusinessCertificate;
            ent.MaxCredit = model.MaxCredit;
            ent.MemberLevel = model.MemberLevel;
            ent.Remark = model.Remark;
            ent.CreateTime = DateTime.Now;
            ent.LastLoginTime = DateTime.Now;
            ent.IsDistribution = model.IsDistribution ? 1 : 0;
            ent.SalesmanID = model.SalesmanID;
            ent.CustomerServiceID = model.CustomerServiceID;
            ent.PaymentMode = model.PaymentMode;

            ent.Contact.ProvinceID = model.ProvinceID;
            ent.Contact.CityID = model.CityID;
            ent.Contact.AreaID = model.AreaID;
            ent.Contact.Address = model.Address;
            ent.Contact.PostalCode = model.PostalCode;
            ent.Contact.Linkman = model.Linkman;
            ent.Contact.LinkmanMobileNo = model.LinkmanMobileNo;
            ent.Contact.LinkmanTel = model.LinkmanTel;
            ent.Contact.LinkmanEmail = model.LinkmanEmail;
            ent.Contact.LinkmanFax = ent.Contact.Fax = model.LinkmanFax;
            ent.EnterpriseType = model.EnterpriseType;
            ent.OwnerID = Config.CompanyID;

            ExecuteResult result;
            if (model.UserID == 0)
            {

                ent.CreateUserID = Config.CurrentOperator.UserID;
                ent.UserNo = "1";
                ent.ParentID = 0;
                ent.PaymentMode = model.PaymentMode;
                ent.State = 1;
                ent.EnterpriseMemberState = (int)EnterpriseMemberState.有效;
                ent.AccountNo = model.AccountNo;
                ent.Password = model.Password;
                ent.MobileNo = model.MobileNo;

                result = account.CreateEnterpriseMember(ent);
            }
            else
            {
                result = account.ModifyEnterpriseMember(ent);
            }

            if (result.ResultNo == 1)
            {
                if (!string.IsNullOrWhiteSpace(model.ReturnUrl))
                {
                    return Redirect(model.ReturnUrl);
                }
                else if (model.act == "save")
                {
                    model.Message = "修改成功,关闭窗口";
                    model.MessageType = MessageType.close;
                    return new CloseResult();
                }
                else
                {
                    model.Message = "修改成功";
                    model.MessageType = MessageType.correct;
                }
            }
            else
            {
                model.Message = result.Message;
                model.MessageType = MessageType.error;
            }

            return RedirectToAction("EditEnterpriseMember", new { UserID = ent.UserID, Message = model.Message, MessageType = model.MessageType });
        }

        #endregion

    }
}
